package com.example.controller;

import com.example.common.Result;
import com.example.entity.Account;
import com.example.entity.Category;
import com.example.exception.CustomerException;
import com.example.service.CategoryService;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@CrossOrigin
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Category> categorys = categoryService.selectAll();
        return Result.success(categorys);
    }
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam Integer pageNum,
                             @RequestParam Integer pageSize,
                             @RequestParam (required = false) String title){
        PageInfo<Category> pageInfo = categoryService.selectPage(pageNum, pageSize,title);
        return Result.success(pageInfo);
    }
    @PostMapping("/add")
    public Result add(@RequestBody Category category) {
        Account currentUser = TokenUtils.getCurrentUser();
        if ("USER".equals(currentUser.getRole())) {
            throw new CustomerException("500", "您的角色暂无权限执行该操作");
        }
        categoryService.add(category);
        return Result.success();
    }
    @PostMapping("/update")
    public Result update(@RequestBody Category category) {
        categoryService.update(category);
        return Result.success();
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        categoryService.deleteById(id);
        return Result.success();
    }
//    @DeleteMapping("/deleteBatch")
//    public Result deleteBatch(@RequestBody List<Category  > list) {
//        categoryService.deleteBatch(list);
//        return Result.success();
//    }
}
